package DBLayer;
import ModelLayer.*;

import java.sql.*;
import java.util.ArrayList;

public class DBProdukt implements IFDBProdukt{
    private  Connection con;
    /** Creates a new instance of DBSupplier */
    public DBProdukt() {
      con = DBConnection.getInstance().getDBcon();
    }
    
 
	//private methods
    //michWere is used whenever we want to select more than one Produkt 
	private ArrayList<Produkt> miscWhere(String wClause, boolean retrieveAssociation)
	{
        ResultSet results;
	    ArrayList<Produkt> list = new ArrayList<Produkt>();	
		
	    String query =  buildQuery(wClause);
	    System.out.println("print noget ud " +query+"");
            try{ // read the produkt from the database
		Statement stmt = con.createStatement();
	 	stmt.setQueryTimeout(5);
	 	results = stmt.executeQuery(query);
	 	
	
		while( results.next() ){
	     	 Produkt prodObj = new Produkt();
		 prodObj = buildProdukt(results);	
                 list.add(prodObj);	
		}//end while
                 stmt.close();     
                 if(retrieveAssociation)
                 {   //The supervisor and department is to be build as well
                     for(Produkt prodObj : list){
                    	 DBLeverandor lev = new DBLeverandor();
                    	 Leverandor lev2 = lev.findLeverandor(prodObj.getLeverandorId().getLeverandorID(), false);
                    	 prodObj.setLeverandorId(lev2);  
                       // here the department has to be selected as well
                     }
                 }//end if   
			
		}//slut try	
	 	catch(Exception e){
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}
	
	
	
	//Singelwhere is used when we only select one employee 	
	private Produkt singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		Produkt prodObj = new Produkt();
		
                
	        String query =  buildQuery(wClause);
                System.out.println(query);
		try{ // read the klasse from the database
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() ){
                            prodObj = buildProdukt(results);
                            //assocaition is to be build
                            stmt.close();
                        
                            if(retrieveAssociation)
                            {
                           	 DBLeverandor lev = new DBLeverandor();
                        	 Leverandor lev2 = lev.findLeverandor(prodObj.getLeverandorId().getLeverandorID(), false);
                        	 prodObj.setLeverandorId(lev2);
                            }
                            
			}
                        else{ //no supplier was found
                            prodObj = null;
                        }
		}//end try	
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return prodObj;
	}
	

	//method to build the query
	private String buildQuery(String wClause)
	{
	    String query="SELECT pris, produktId, minGreanse, Beskrivelse, VareNavn, LeverandorId FROM produkter";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}
	
	
	
	//metode til at opbygge et objekt Produkt fra Databasen
	private Produkt buildProdukt (ResultSet results)
      {   
		Produkt prodObj = new Produkt();
      
          try{ // Coloumnnave skal skrives her
                prodObj.setPris(results.getInt("Pris"));
                prodObj.setProduktId(results.getInt("produktId"));
                prodObj.setMinLimit(results.getInt("MinGreanse"));
                prodObj.setBeskrivelse(results.getString("Beskrivelse"));
                prodObj.setProduktNavn(results.getString("VareNavn"));    
                
                
                prodObj.setLeverandorId(new Leverandor(null, null, results.getInt("leverandorId")));
                
                
                
          }
         catch(Exception e)
         {
             System.out.println("error in building the product object");
         }
         return prodObj;
      }


   // public ArrayList<Produkt> getAllProducts(boolean retriveAssociation)
    //{
    	
    //    return miscWhere("", retriveAssociation);
    //}
    
    
	
    //finder 1 produkt via 1 id
    public Produkt findProduktId(int pId, boolean retriveAssociation)
    {   String wClause = "  produktId = '" + pId + "'";
        return singleWhere(wClause, retriveAssociation);
    }
    
    
    //finder 1 produkt via navn 
    public ArrayList<Produkt> searchProductName(String vareNavn, boolean retriveAssociation)
    {
        String wClause = "VareNavn like '%" + vareNavn + "%'";
        System.out.println("SearchProdukt " + wClause);
        return miscWhere(wClause, retriveAssociation);
    }

    public Produkt findLeverandorId(int leverandorId, boolean retriveAssociation)
    {
    	String wClause = " leverandorId = '" + leverandorId + "'";
    	return singleWhere(wClause, retriveAssociation);
    }
    
	@Override
	public int insertProdukt(Produkt p, boolean b) {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public int deleteProdukt(int produktId) {
		// TODO Auto-generated method stub
		return 0;
	}
 
}